from fastapi import APIRouter, Depends

from apps.system import cruds, schemas
from core.auth import Auth, FullAdminAuth
from core.dependencies import IdList
from core.response import SuccessResponse

router = APIRouter(prefix="/menu")


@router.get("/list", summary="获取菜单列表")
async def list_menus(
        auth: Auth = Depends(FullAdminAuth(permissions=["auth.menu.list"]))
):
    datas = await cruds.MenuDal(auth.db).get_tree_list(mode=1)
    return SuccessResponse(datas)


@router.get("/options")
async def list_menu_options(
        auth: Auth = Depends(FullAdminAuth())
):
    return SuccessResponse(await cruds.MenuDal(auth.db).get_tree_list(mode=3))


@router.post("/create", summary="创建菜单")
async def create_menu(
        menu: schemas.Menu,
        auth: Auth = Depends(FullAdminAuth(permissions=["system.menu.create"]))
):
    if menu.parent_id:
        menu.alwaysShow = False
    return SuccessResponse(
        await cruds.MenuDal(auth.db).create_data(data=menu),
        msg="创建成功"
    )


@router.put("/update/{data_id}", summary="更新菜单")
async def update_menu(
        data_id: int,
        menu: schemas.Menu,
        auth: Auth = Depends(FullAdminAuth(permissions=["system.menu.update"]))
):
    if menu.parent_id:
        menu.alwaysShow = False
    return SuccessResponse(
        await cruds.MenuDal(auth.db).put_data(data_id, menu),
        msg="修改成功"
    )


@router.delete("/delete", summary="批量删除菜单")
async def delete_menu(
        ids: IdList = Depends(),
        auth: Auth = Depends(FullAdminAuth(permissions=["system.menu.delete"]))
):
    await cruds.MenuDal(auth.db).delete_datas(ids.ids, v_soft=False)
    return SuccessResponse("删除成功")
